<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\agent;

use App\Dao\AgentBankDao;
use App\Request\Agent\UserBank\AddBankRequest;
use App\Service\Agent\UserBankService;
use App\Middleware\User\AgentMiddleware;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 用户银行卡控制器
 *
 * @AutoController()
 * @Middleware(AgentMiddleware::class)
 * @author 张昊(2396675557@qq.com)
 * @package App\Controller\admin
 */
class BankController extends Controller
{
    /**
     * 用户服务
     *
     * @Inject
     * @var UserBankService
     */
    private $service;

    /**
     * 用户银行卡 Dao
     *
     * @Inject
     * @var AgentBankDao
     */
    private $dao;

    /**
     * 获取银行卡列表
     *
     * @author 刘兴永(aile8880@qq.com)
     * @date 2020-02-22 19:57:00
     */
    public function get()
    {
        $result = $this->service->dao->get();

        $this->success($result);
    }

    /**
     * 增加银行卡
     *
     * @param AddBankRequest $request
     */
    public function add(AddBankRequest $request)
    {
        $params = $request->all();

        $create = $this->service->dao->create([
            'agent_id' => agentId(),
            'bank_name' => $params['bank_name'],
            'bank_account' => $params['bank_account'],
            'name' => $params['name'],
            'idcard' => $params['idcard'],
            'phone' => $params['phone'],
        ]);

        !$create && $this->error();

        $this->success();
    }

    /**
     * 删除银行卡接口
     *
     * @author 刘兴永(aile8880@qq.com)
     * @date 2020-02-22 19:57:00
     */
    public function del()
    {
        $bank_id = (int)$this->request->input('bank_id', 0);
        if ($bank_id === 0) $this->error();

        $this->service->dao->delete($bank_id);

        $this->success();
    }
}